<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="1:收到直播礼物2：提现3:提现失败4申请商家1:收到直播礼物2：提现3:提现失败4：申请商家5：审核未通过退款" prop="source">
      <el-input v-model="dataForm.source" placeholder="1:收到直播礼物2：提现3:提现失败4申请商家1:收到直播礼物2：提现3:提现失败4：申请商家5：审核未通过退款"></el-input>
    </el-form-item>
    <el-form-item label="1代表收入0代表支出" prop="type">
      <el-input v-model="dataForm.type" placeholder="1代表收入0代表支出"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="操作数量" prop="num">
      <el-input v-model="dataForm.num" placeholder="操作数量"></el-input>
    </el-form-item>
    <el-form-item label="现有余额" prop="nowNum">
      <el-input v-model="dataForm.nowNum" placeholder="现有余额"></el-input>
    </el-form-item>
    <el-form-item label="详细记录" prop="detailRemark">
      <el-input v-model="dataForm.detailRemark" placeholder="详细记录"></el-input>
    </el-form-item>
    <el-form-item label="订单编号" prop="orderNumber">
      <el-input v-model="dataForm.orderNumber" placeholder="订单编号"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          createTime: '',
          source: '',
          type: '',
          remark: '',
          num: '',
          nowNum: '',
          detailRemark: '',
          orderNumber: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          source: [
            { required: true, message: '1:收到直播礼物2：提现3:提现失败4申请商家1:收到直播礼物2：提现3:提现失败4：申请商家5：审核未通过退款不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '1代表收入0代表支出不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          num: [
            { required: true, message: '操作数量不能为空', trigger: 'blur' }
          ],
          nowNum: [
            { required: true, message: '现有余额不能为空', trigger: 'blur' }
          ],
          detailRemark: [
            { required: true, message: '详细记录不能为空', trigger: 'blur' }
          ],
          orderNumber: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/balancelog/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.balanceLog.userId
                this.dataForm.createTime = data.balanceLog.createTime
                this.dataForm.source = data.balanceLog.source
                this.dataForm.type = data.balanceLog.type
                this.dataForm.remark = data.balanceLog.remark
                this.dataForm.num = data.balanceLog.num
                this.dataForm.nowNum = data.balanceLog.nowNum
                this.dataForm.detailRemark = data.balanceLog.detailRemark
                this.dataForm.orderNumber = data.balanceLog.orderNumber
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/balancelog/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'createTime': this.dataForm.createTime,
                'source': this.dataForm.source,
                'type': this.dataForm.type,
                'remark': this.dataForm.remark,
                'num': this.dataForm.num,
                'nowNum': this.dataForm.nowNum,
                'detailRemark': this.dataForm.detailRemark,
                'orderNumber': this.dataForm.orderNumber
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
